﻿using AutoMapper;
using Dm;
using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;
using WarehouseManagement.Application.Command.OutboundShipmentsCommand;
using WarehouseManagement.Domain.DTO;
using WarehouseManagement.Domain.DTO.OutvoundRequests;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.OutboundShipmentsCommandHandlers
{
    public class OutboundShipmentsUpdateCommandHandlers : IRequestHandler<OutboundShipmentsUpdateCommand, int>
    {
        protected readonly IMapper mapper;
        protected readonly IOutboundRequestRepository outbound;// 出库请求仓储接口
        protected readonly IOutboundRProductRepository outboundR; // 仓库出库申请产品关系仓储接口
        protected readonly ISqlSugarClient db;

        public OutboundShipmentsUpdateCommandHandlers(IMapper mapper, IOutboundRequestRepository outbound, IOutboundRProductRepository outboundR, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.outbound = outbound;
            this.outboundR = outboundR;
            this.db = db;
        }

        public async Task<int> Handle(OutboundShipmentsUpdateCommand request, CancellationToken cancellationToken)
        {
            db.Ado.BeginTran();
            try
            {
                await outbound.UpdateAsync(mapper.Map<OutboundRequest>(request.dtos));
                var newout = (await outboundR.GetAll()).Where(x => x.OutboundNumber == request.dtos.OutboundNumber).ToList();
                //先删除
                await outboundR.DelRange(newout);
                foreach (var item in request.dtos.oroDto)
                {
                    OutboundRProduct orp = new OutboundRProduct()
                    {
                        ProductId = item.ProductId,
                        OutboundNumber = request.dtos.OutboundNumber,
                        ORQuantityNum = item.ORQuantityNum,
                        ORQPrice = item.ORQPrice,
                        AddName = request.dtos.AddName,
                        AddTime = DateTime.Now,
                        DelName = request.dtos.DelName,
                        DelTime = request.dtos.DelTime,
                        IsDel = request.dtos.IsDel,
                        Office_Id = request.dtos.Office_Id,
                        User_Id = request.dtos.User_Id,
                        UpdName = request.dtos.UpdName,
                        UpdTime = request.dtos.UpdTime
                    };
                    await outboundR.AddAsync(orp);
                }
                MessageCreateDto dto = new MessageCreateDto();
                dto.MType = "出库申请";
                dto.Title = "出库申请修改";
                dto.SendTime = DateTime.Now;
                await MessageCreates(dto);
                db.Ado.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                db.Ado.RollbackTran();
                throw;
            }
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public async Task<int> MessageCreates(MessageCreateDto dtos)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://116.205.121.197:8000/erpcp/api/Message/SendMessage";
            string jsonstr = JsonConvert.SerializeObject(dtos);
            string postReponse = await cilent.PostAsync(chanpin, jsonstr);
            return 1;
        }
    }
}
